layui.use(['laytpl','jquery','laydate','layer','form','table'],function(layptl,$,laydate,layer,form,table){

   var schoolId = null;
   var data = null;
   var data3 = null;

    $.post({
        url:"/school/getAllSchoolName",
        async:false,
        success: function (res) {
            // console.log(res);
            data3 = res;
        }
    });

    // var laytpl = layui.laytpl;
    //建立视图。用于呈现渲染结果。
    var gettp3 = document.getElementById('testTp3').innerHTML;
    layptl(gettp3).render(data3, function (html) {
        // console.log(html)
        //得到的模板渲染到html
        document.getElementById('test1').innerHTML = html;
    });

    // var form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
    form.render();
    form.render('select' ,'test'); //刷新select选择框渲染

    form.on('select(mySelect1)', function (data) {
        schoolId = data.value;

        $.post({
            url:"/sys/querySemeBySchid?schoolId=" + schoolId,
            async:false,
            success: function (res) {
                // console.log(res);
                data = res;
            }
        });

        // var laytpl = layui.laytpl;
        //建立视图。用于呈现渲染结果。
        var gettpl1 = document.getElementById('testTpl').innerHTML;
        layptl(gettpl1).render(data, function (html) {
            // console.log(html)
            //得到的模板渲染到html
            document.getElementById('course').innerHTML = html;
        });

        lay('.test-item').each(function(d) {
            laydate.render({
                elem: this
                ,range: true
                ,format: 'yyyy.MM.dd'	//日期格式化
                ,done: function(value1){
                    // alert(d);
                    var semesterID = data[d].semesterID;
                    var times = value1.split(' - ');
                    var start = times[0].replace('.','-').replace('.','-');
                    var end = times[1].replace('.','-').replace('.','-');
                    // Url：localhost:80/sys/editSemesterDate?schoolId=1&semesterID=2019-2020-2&stTime=2019-08-01&edTime=2020-11-2
                    $.post({
                        url:"/sys/editSemesterDate?schoolId=" + schoolId + "&semesterID=" + semesterID + "&stTime=" + start + "&edTime=" + end ,
                        async:false,
                        success: function (res) {
                            // alert(res)
                            if(res == 1) {
                                layer.msg('修改成功',{icon:7,time:800,shade:[0.6,'#000',true]});
                            }else {
                                // layer.alert('您设置的时间区间有误，修改失败',300)
                                top.layer.msg('您设置的时间区间有误，修改失败',{icon:7,time:800,shade:[0.6,'#000',true]});
                            }
                        }
                    });
                }
            });

        });

    });


        //日期范围
        laydate.render({
            elem: '#add2'
            ,range: true
            ,format: 'yyyy.MM.dd'	//日期格式化
        });

        $.post({
            url:"/sys/querySemeBySchid?schoolId=" + schoolId,
            async:false,
            success: function (res) {
                // console.log(res);
                data = res;
            }
        });

        // var laytpl = layui.laytpl;
        //建立视图。用于呈现渲染结果。
        var gettpl = document.getElementById('testTpl').innerHTML;
        layptl(gettpl).render(data, function (html) {
            // console.log(html)
            //得到的模板渲染到html
            document.getElementById('course').innerHTML = html;
        });

    lay('.test-item').each(function(d) {
        laydate.render({
            elem: this
            ,range: true
            ,format: 'yyyy.MM.dd'	//日期格式化
            ,done: function(value1){
                // alert(d);
                var semesterID = data[d].semesterID;
                var times = value1.split(' - ');
                var start = times[0].replace('.','-').replace('.','-');
                var end = times[1].replace('.','-').replace('.','-');
                // Url：localhost:80/sys/editSemesterDate?schoolId=1&semesterID=2019-2020-2&stTime=2019-08-01&edTime=2020-11-2
                $.post({
                    url:"/sys/editSemesterDate?schoolId=" + schoolId + "&semesterID=" + semesterID + "&stTime=" + start + "&edTime=" + end ,
                    async:false,
                    success: function (res) {
                        // alert(res)
                        if(res == 1) {
                            layer.msg('修改成功',{icon:7,time:800,shade:[0.6,'#000',true]});
                        }else {
                            // layer.alert('您设置的时间区间有误，修改失败',300)
                            top.layer.msg('您设置的时间区间有误，修改失败',{icon:7,time:800,shade:[0.6,'#000',true]});
                        }
                    }
                });
            }
        });

    });


    $(function () {
        //监听新增按钮
        $("#add").click(function () {
            if(schoolId != null) {
                mainIndex = layer.open({
                    title : ['新增学期','font-size:18px;text-align:center;color:blue;'],
                    type : 1,  //yw 使用这种方式从服务器下载更少数据12kB,处理速度更快47毫秒，提高了并发性
                    content : $("#addDiv"),
                    // type : 2,  //yw 使用这种方式从服务器下载更多数据116kB，处理速度更慢264毫秒，减少了并发性
                    // content : "adminMagSave.html",
                    area:['500px','250px'],
                    offset:'r',
                    success:function(){ //yw layero是弹出层对象layer object

                    }
                });

            }else {
                layer.msg('请先选择学校！',{icon:7,time:800,shade:[0.6,'#000',true]});
            }

        });

        // window.onload = function () {
            form.on("submit(doSubmitSave)",function(){
                // alert("xxx")
                //获得新增学期的学期名称并去除空格
                var name = $("#add1").val().replace(/\s*/g, '');
                var date = $("#add2").val().replace(/\s*/g, '');

                //对name进行参数校验
                var patt1=new RegExp("2[0-9][0-9][0-9]-2[0-9][0-9][0-9]-[0-9]+");
                var patt2=new RegExp("2[0-9][0-9][0-9]-[0-9]+");
                var patt3=new RegExp("2[0-9][0-9][0-9].[0-1][0-9].[0-3][0-9]-2[0-9][0-9][0-9].[0-1][0-9].[0-3][0-9]");
                if(!(patt1.test(name) || patt2.test(name))) {
                    layer.msg('学期名称格式错误！',{icon:7,time:800,shade:[0.6,'#000',true]});
                    return false;
                }
                if(!patt3.test(date)) {
                    layer.msg('学期时间格式错误！',{icon:7,time:800,shade:[0.6,'#000',true]});
                    return false;
                }
                // localhost:80/sys/setSemesterDate?schoolId=1&semesterID=2019-2020-1&stTime=2019-09-01&2020-10-2
                var times = date.split('-');
                var start = times[0].replace('.','-').replace('.','-');
                var end = times[1].replace('.','-').replace('.','-');
                $.post({
                    url:"/sys/setSemesterDate?schoolId=" + schoolId + "&semesterID=" + name + "&stTime=" + start + "&edTime=" + end ,
                    async:false,
                    success: function (res) {
                        // alert(res);
                        if(res == 1) {
                            layer.msg('修改成功',{icon:7,time:800,shade:[0.6,'#000',true]});

                            $.post({
                                url:"/sys/querySemeBySchid?schoolId=" + schoolId,
                                async:false,
                                success: function (res) {
                                    // console.log(res);
                                    data = res;
                                }
                            });

                            // var laytpl = layui.laytpl;
                            //建立视图。用于呈现渲染结果。
                            var gettpl = document.getElementById('testTpl').innerHTML;
                            layptl(gettpl).render(data, function (html) {
                                // console.log(html)
                                //得到的模板渲染到html
                                document.getElementById('course').innerHTML = html;
                            });

                            layer.closeAll('page'); //yw 关闭新增界面 type=1

                            lay('.test-item').each(function(d) {
                                laydate.render({
                                    elem: this
                                    ,range: true
                                    ,format: 'yyyy.MM.dd'	//日期格式化
                                    ,done: function(value1){
                                        // alert(d);
                                        var semesterID = data[d].semesterID;
                                        var times = value1.split(' - ');
                                        var start = times[0].replace('.','-').replace('.','-');
                                        var end = times[1].replace('.','-').replace('.','-');
                                        // Url：localhost:80/sys/editSemesterDate?schoolId=1&semesterID=2019-2020-2&stTime=2019-08-01&edTime=2020-11-2
                                        $.post({
                                            url:"/sys/editSemesterDate?schoolId=" + schoolId + "&semesterID=" + semesterID + "&stTime=" + start + "&edTime=" + end ,
                                            async:false,
                                            success: function (res) {
                                                // alert(res)
                                                if(res == 1) {
                                                    layer.msg('修改成功',{icon:7,time:800,shade:[0.6,'#000',true]});
                                                }else {
                                                    // layer.alert('您设置的时间区间有误，修改失败',300)
                                                    top.layer.msg('您设置的时间区间有误，修改失败',{icon:7,time:800,shade:[0.6,'#000',true]});
                                                }
                                            }
                                        });
                                    }
                                });

                            });

                            // return false;
                        }else {
                            // layer.alert('您设置的时间区间有误，修改失败',300)
                            top.layer.msg('您设置的时间区间有误，修改失败',{icon:7,time:800,shade:[0.6,'#000',true]});
                        }
                    }
                });

            });
        // }

        //我是删除
        $(document).on("click",".del",function(){
            // console.log();
            var val = $(this).attr("data");
            // Url：localhost:80/sys/delSemesterDate?schoolId=1&semesterID=2019-2020-2
            $.post({
                url:"/sys/delSemesterDate?schoolId=" + schoolId + "&semesterID=" + val,
                async:false,
                success: function (res) {
                    // alert(res);
                    if(res > 0) {
                        layer.msg('删除成功',{icon:7,time:800,shade:[0.6,'#000',true]});

                        $.post({
                            url:"/sys/querySemeBySchid?schoolId=" + schoolId,
                            async:false,
                            success: function (res) {
                                // console.log(res);
                                data = res;
                            }
                        });

                        // var laytpl = layui.laytpl;
                        //建立视图。用于呈现渲染结果。
                        var gettpl = document.getElementById('testTpl').innerHTML;
                        layptl(gettpl).render(data, function (html) {
                            // console.log(html)
                            //得到的模板渲染到html
                            document.getElementById('course').innerHTML = html;
                        });

                        lay('.test-item').each(function(d) {
                            laydate.render({
                                elem: this
                                ,range: true
                                ,format: 'yyyy.MM.dd'	//日期格式化
                                ,done: function(value1){
                                    // alert(d);
                                    var semesterID = data[d].semesterID;
                                    var times = value1.split(' - ');
                                    var start = times[0].replace('.','-').replace('.','-');
                                    var end = times[1].replace('.','-').replace('.','-');
                                    // Url：localhost:80/sys/editSemesterDate?schoolId=1&semesterID=2019-2020-2&stTime=2019-08-01&edTime=2020-11-2
                                    $.post({
                                        url:"/sys/editSemesterDate?schoolId=" + schoolId + "&semesterID=" + semesterID + "&stTime=" + start + "&edTime=" + end ,
                                        async:false,
                                        success: function (res) {
                                            // alert(res)
                                            if(res == 1) {
                                                layer.msg('修改成功',{icon:7,time:800,shade:[0.6,'#000',true]});
                                            }else {
                                                // layer.alert('您设置的时间区间有误，修改失败',300)
                                                top.layer.msg('您设置的时间区间有误，修改失败',{icon:7,time:800,shade:[0.6,'#000',true]});
                                            }
                                        }
                                    });
                                }
                            });

                        });

                        // return false;
                    }else {
                        // layer.alert('您设置的时间区间有误，修改失败',300)
                        top.layer.msg('删除失败',{icon:7,time:800,shade:[0.6,'#000',true]});
                    }
                }
            });

        })
    });
});
